<?php
    if ( ! defined('BASEPATH'))
        exit('No direct script access allowed');
    class Problem extends CCMIS_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function index()
        {
            $this->setup_view('pfmis/pfmis/problem/view');
        }
        public function addproblem()
        {
            if($this->input->server('REQUEST_METHOD') == 'GET')
                $this->setup_view('pfmis/pfmis/problem/add');
            else
            {
                if(isset($_POST['room']))
                {
                    $data['room'] = $_POST['room'];
                    $this->setup_view('pfmis/pfmis/problem/getfacility',$data);
                }
                else
                {
                    $this->load->database();
                    $arrayNameType = explode(' - ',$_POST['item']);
                    $getMinIdQ = 'SELECT MIN(FacilityID) as MinF FROM pf_facilities WHERE FacilityName = \''.$arrayNameType[0].'\' AND FacilityType = \''.$arrayNameType[1].'\' AND Status = 1';
                    $getMinIdR = $this->db->query($getMinIdQ);
                    $getMinId = $getMinIdR->result();
                    
                    $getMaxIdQ = 'SELECT MAX(CasualtyID) as MaxC FROM pf_casualty';
                    $getMaxIdR = $this->db->query($getMaxIdQ);
                    $getMaxId = $getMaxIdR->result();
                                            
                    $addCasualty = 'INSERT INTO pf_casualty VALUES (\''.(($getMaxId[0]->MaxC)+1).'\',\''.$getMinId[0]->MinF.'\',\''.date('Y-m-d').'\',\''.$_POST['desc'].'\',\'0\')';
                    $this->db->query($addCasualty);
                    
                    $status = -1;
                    if($_POST['repair'] == true)
                    {
                        echo 'pasok dito';
                        $status = 0;
                    }
                    $updateFacility = 'UPDATE pf_facilities SET Status = '.$status.' WHERE FacilityID = \''.$getMinId[0]->MinF.'\'';
                    $this->db->query($updateFacility);
                    $this->setup_view('pfmis/pfmis/problem/view');
                }
            }
        }
        public function repair()
        {
            if($this->input->server('REQUEST_METHOD') == 'GET')
                $this->setup_view('pfmis/pfmis/problem/repair');
            else
            {
                $this->load->database();
                $updateFacility = 'UPDATE pf_facilities SET Status = 1 WHERE FacilityID = \''.$_POST['facility'].'\'';
                echo $updateFacility;
                $this->db->query($updateFacility);
                
                $getMaxIDQ = 'SELECT MAX(ExpenditureID) as MAX FROM pf_expenditure';
                $getMaxIDR = $this->db->query($getMaxIDQ);
                $getMaxID = $getMaxIDR->result();
                
                $addExpenditureQ = 'INSERT INTO pf_expenditure VALUES(\''.(($getMaxID[0]->MAX)+1).'\',\''.$_POST['facility'].'\',\'REPAIR\',\''.date('Y-m-d').'\',\'1\',\''.$_POST['price'].'\',\''.$_POST['facility'].'\')';
                $this->db->query($addExpenditureQ);
                $this->setup_view('pfmis/pfmis/problem/view');
                
                $updateCasualtyQ = 'UPDATE pf_casualty SET Status = 1 WHERE FacilityID = \''.$_POST['facility'].'\'';
                $this->db->query($updateCasualtyQ);
            }
        }
        public function dispose()
        {
            $this->setup_view('pfmis/pfmis/problem/dispose');
        }
    }
?>